<?php 
/**
 * @package framework
 * @subpackage controllers
 *
 * @author Philippe Archambault <philippe.archambault@gmail.com>
 * @version 0.1
 * @license http://www.gnu.org/licenses/gpl.html GPL License
 * @copyright Philippe Archambault, 2008
 */

/**
 * Class CareerController
 *
 * @package framework
 * @subpackage controllers
 *
 * @since 0.1
 */
class CareerController extends Controller
{

    public function __construct()
    {
        AuthUser::load();
        if ( ! AuthUser::isLoggedIn())
            redirect(get_url('login'));
        $this->setLayout('backend');
        $this->assignToLayout('sidebar', new View('career/sidebar'));
    }
    
    public function index()
    {
		
        if ( ! AuthUser::hasPermission('administrator'))
        {
            Flash::set('error', __('You do not have permission to access the requested page!'));

            if (Setting::get('default_tab') === 'career')
                redirect(get_url('page'));
            else
                redirect(get_url());
        }
        
        $this->display('career/index', array(
            'careers' => Career::findAll()
        ));
    }
    
    public function add()
    {
        if ( ! AuthUser::hasPermission('administrator'))
        {
            Flash::set('error', __('You do not have permission to access the requested page!'));
            redirect(get_url());
        }
        
        // check if trying to save
        if (get_request_method() == 'POST')
            return $this->_add();
        
        // check if career have already enter something
        $career = Flash::get('post_data');
        
        if (empty($career))
            $career = new Career;
        
        $this->display('career/edit', array(
            'action' => 'add',
            'locatioin' => $career,
            'permissions' => Record::findAllFrom('Permission')
        ));
    }
    
    private function _add()
    {
        $data = $_POST['career'];
		
		Flash::set('post_data', (object) $data);
        
        // check if name > 0 chars
        if (empty($data['name']))
        {
            Flash::set('error', __('You have to specify a name!'));
            redirect(get_url('career/add'));
        }
		
		// check if location > 0 chars
        if (empty($data['location']))
        {
            Flash::set('error', __('You have to specify a location!'));
            redirect(get_url('career/add'));
        }
		
		// check if position_available > 0 chars
        if (empty($data['position_available']))
        {
            Flash::set('error', __('You have to specify a position available!'));
            redirect(get_url('career/add'));
        }
		
		// check if contact_person > 0 chars
        if (empty($data['contact_person']))
        {
            Flash::set('error', __('You have to specify a contact person!'));
            redirect(get_url('career/add'));
        }
		
		// check if salary > 0 chars
        if (empty($data['salary']))
        {
            Flash::set('error', __('You have to specify a salary!'));
            redirect(get_url('career/add'));
        }
		
		// check if commission > 0 chars
        if (empty($data['commission']))
        {
            Flash::set('error', __('You have to specify a commission!'));
            redirect(get_url('career/add'));
        }
		
		// check if description > 0 chars
        if (empty($data['description']))
        {
            Flash::set('error', __('You have to specify a description!'));
            redirect(get_url('career/add'));
        }
		
		if (!array_key_exists('share_to_social_site', $data)) {
			$data['share_to_social_site'] = 0;
		} else {
			$data['share_to_social_site'] = 1;
		}
        
        $career = new Career($data);
		
        if ($career->save())
        {
            Flash::set('success', __('Career has been added!'));
            Observer::notify('career_after_add', $career->name);
        }
        else Flash::set('error', __('Career has not been added!'));
        
        redirect(get_url('career'));
    }
    
    public function edit($id)
    {
        if ( AuthUser::getId() != $id && ! AuthUser::hasPermission('administrator'))
        {
            Flash::set('error', __('You do not have permission to access the requested page!'));
            redirect(get_url());
        }
        
        // check if trying to save
        if (get_request_method() == 'POST')
            return $this->_edit($id);
        
        if ($career = Career::findById($id))
        {
            $this->display('career/edit', array(
                'action' => 'edit', 
                'career' => $career,
                'permissions' => Record::findAllFrom('Permission')
            ));
        }
        else Flash::set('error', __('Career not found!'));
        
        redirect(get_url('career'));
        
    } // edit
    
    private function _edit($id)
    {
        $data = $_POST['career'];
		
        // check if name > 0 chars
        if (empty($data['name']))
        {
            Flash::set('error', __('You have to specify a name!'));
            redirect(get_url('career/add'));
        }
		
		// check if location > 0 chars
        if (empty($data['location']))
        {
            Flash::set('error', __('You have to specify a location!'));
            redirect(get_url('career/add'));
        }
		
		// check if position_available > 0 chars
        if (empty($data['position_available']))
        {
            Flash::set('error', __('You have to specify a position available!'));
            redirect(get_url('career/add'));
        }
		
		// check if contact_person > 0 chars
        if (empty($data['contact_person']))
        {
            Flash::set('error', __('You have to specify a contact person!'));
            redirect(get_url('career/add'));
        }
		
		// check if salary > 0 chars
        if (empty($data['salary']))
        {
            Flash::set('error', __('You have to specify a salary!'));
            redirect(get_url('career/add'));
        }
		
		// check if commission > 0 chars
        if (empty($data['commission']))
        {
            Flash::set('error', __('You have to specify a commission!'));
            redirect(get_url('career/add'));
        }
		
		// check if description > 0 chars
        if (empty($data['description']))
        {
            Flash::set('error', __('You have to specify a description!'));
            redirect(get_url('career/add'));
        }
		
		if (!array_key_exists('share_to_social_site', $data)) {
			$data['share_to_social_site'] = 0;
		} else {
			$data['share_to_social_site'] = 1;
		}
        
        $career = Record::findByIdFrom('Career', $id);
        $career->setFromData($data);
        
        if ($career->save())
        {
            Flash::set('success', __('Career has been saved!'));
            Observer::notify('career_after_edit', $career->name);
        }
        else Flash::set('error', __('Career has not been saved!'));
        
            redirect(get_url('career'));
    }
    
    public function delete($id)
    {
        if ( ! AuthUser::hasPermission('administrator'))
        {
            Flash::set('error', __('You do not have permission to access the requested page!'));
            redirect(get_url());
        }
        
		// find the career to delete
		if ($career = Record::findByIdFrom('Career', $id))
		{
			if ($career->delete())
			{
				Flash::set('success', __('Career <strong>:name</strong> has been deleted!', array(':name' => $career->name)));
				Observer::notify('career_after_delete', $career->name);
			}
			else
				Flash::set('error', __('Career <strong>:name</strong> has not been deleted!', array(':name' => $career->name)));
		}
		else Flash::set('error', __('Career not found!'));
        
        
        redirect(get_url('career'));
    }

} // end CareerController class
